<template>
    <el-card>
        <p>当前系统时间：{{ time }}</p>
        <p>过期时间：{{ expireTime }}</p>
    </el-card>
    <el-dialog v-model="messageDialog" title="生成用户" width="30%">
        <span>用户名:{{ newUser.id }}</span>
        <span>密码: admin</span>
    </el-dialog>
</template>
<script>
import { getRequest, postRequest } from '@/utils/http';
import { message } from '@/utils/messageBox';
import { ref } from 'vue';

export default {
    setup() {
        let timestamp = ref();
        let time = ref('');
        let timer;
        let expireTime = ref('');

        const timestampToTime = (timestamp) => {
            var date = new Date(timestamp);
            var Y = date.getFullYear() + "-";
            var M =
                (date.getMonth() + 1 < 10
                    ? "0" + (date.getMonth() + 1)
                    : date.getMonth() + 1) + "-";
            var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
            var h = date.getHours().toString().padStart(2, 0) + ":";
            var m = date.getMinutes().toString().padStart(2, 0) + ":";
            var s = date.getSeconds().toString().padStart(2, 0);
            return Y + M + D + h + m + s;
        }

        const getTime = () => {
            getRequest("/config/systemTime", {},
                function success(resp) {
                    if (resp.code == '200') {
                        timestamp.value = resp.data;
                        time.value = timestampToTime(timestamp.value);
                    } else {
                        message(resp.msg, 'error');
                    }
                }, function error() {
                    message('获取系统时间出错', 'error');
                })
        }

        const getExpire = () => {
            postRequest("/company/getExpired", {
            }, function success(resp) {
                if (resp.code == '200') {
                    expireTime.value = resp.data;
                } else {
                    message(resp.msg, 'error');
                }
            }, function error(resp) {
                message(resp.msg, 'error');
            }
            )
        }
        getTime();
        getExpire();

        return {
            time,
            expireTime,
            timer,
            timestamp,
            timestampToTime,
        }
    },
    methods: {
        timeFormatter(row, column, cellValue) {
            let format = 'YYYY-mm-dd HH:MM:SS'
            let date = new Date(cellValue);
            const dataItem = {
                'Y+': date.getFullYear().toString(),
                'm+': (date.getMonth() + 1).toString(),
                'd+': date.getDate().toString(),
                'H+': date.getHours().toString(),
                'M+': date.getMinutes().toString(),
                'S+': date.getSeconds().toString(),
            };
            Object.keys(dataItem).forEach((item) => {
                const ret = new RegExp(`(${item})`).exec(format);
                if (ret) {
                    format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
                }
            });
            return format
        },
    },
    created() {
        this.timer = setInterval(() => {
            this.timestamp += 1000;
            this.time = this.timestampToTime(this.timestamp);
        }, 1000)
    },
    beforeRouteLeave() {
        clearInterval(this.timer);
    },

}
</script>

<style scoped></style>